# XSLT Extensions

import amplitude
from lxml import etree
from copy import deepcopy

class Uppercase(etree.XSLTExtension):
    def execute(self, c, self_node, input_node, output_parent):
        output_parent.extend( [self.upper(deepcopy(input_node))] )
    def upper(self, nodes):
        if type(nodes)==etree._Element:
            nodes.text = nodes.text.upper()
        for node in list(nodes):
            node.text = (node.text or '').upper()
            # node.tail = (node.tail or '').upper()
            self.upper(list(node))
        return nodes


class Lowercase(etree.XSLTExtension):
    def execute(self, c, self_node, input_node, output_parent):
        output_parent.extend( [self.lower(deepcopy(input_node))] )
    def lower(self, nodes):
        if type(nodes)==etree._Element:
            nodes.text = nodes.text.lower()
        for node in list(nodes):
            node.text = (node.text or '').lower()
            # node.tail = (node.tail or '').lower()
            self.lower(list(node))
        return nodes

URL=amplitude.NAMESPACE['url']
xslt_extensions = {
    # (URL, 'lowercase'): Lowercase(),
    # (URL, 'uppercase'): Uppercase(),
}
